const createListNode = arr => {
  function ListNode(val, next) {
    this.val = val === undefined ? 0 : val
    this.next = next === undefined ? null : next
  }
  let oldListNode = null
  for (let index = arr.length - 1; index > -1; index--) {
    const val = arr[index]
    const newListNode = new ListNode(val, oldListNode)
    oldListNode = newListNode
  }
  return oldListNode
}
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */
var rotateRight = function (head, k) {
  if (!head || !head.next) {
    return head
  }
  let headlength = 1
  let temhead = head
  while (temhead.next) {
    temhead = temhead.next
    headlength++
  }
  const getNodeToFirst = head => {
    let temhead = head
    while (temhead.next.next) {
      temhead = temhead.next
    }
    let node = temhead.next
    temhead.next = null
    node.next = head
    return node
  }
  debugger
  k = k % headlength
  while (k > 0) {
    k--
    head = getNodeToFirst(head)
  }
  return head
}
console.log(rotateRight(createListNode([1, 2, 3]), 200000000))
